//
//  babyMessageViewController.m
//  cherryproduct
//
//  Created by Enter zhao on 14-8-18.
//  Copyright (c) 2014年 cherry. All rights reserved.
//

#import "babyMessageViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface babyMessageViewController ()<UIAlertViewDelegate>

@property(nonatomic, assign) NSInteger max_moth;
@property(nonatomic, assign) NSInteger max_day;

@end

@implementation babyMessageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.myView.hidden = YES;
    [self setTitle:@"宝宝信息"];
    _boyButton.backgroundColor = _segementView.backgroundColor;
    [_boyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _girlButton.backgroundColor = [UIColor whiteColor];
    [_girlButton setTitleColor:_segementView.backgroundColor forState:UIControlStateNormal];
    self.view.backgroundColor = [UIColor whiteColor];
    _yearLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"biankuang"]];
    _monthLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"biankuang"]];
    
    _yearLabel.text = _year;
    _monthLabel.text = _month;
    
    self.max_moth=12;
    self.max_day=31;
    
    {
        self.view_year.layer.borderWidth = 1;
        self.view_year.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        
        self.view_moth.layer.borderWidth = 1;
        self.view_moth.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        
        self.view_day.layer.borderWidth = 1;
        self.view_day.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    }
    
    NSDate * date = [NSDate date];
    if (self.str_boday==nil) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        self.str_boday = [dateFormatter stringFromDate:date];
    }
    
    if (self.str_boday == nil || [self.str_boday isKindOfClass:[NSNull class]]) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        self.str_boday = [dateFormatter stringFromDate:date];
    }

    if (self.str_boday) {
        NSArray * array = [self.str_boday componentsSeparatedByString:@"-"];
        if ([array count]<3) {
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
            self.str_boday = [dateFormatter stringFromDate:date];
            array = [self.str_boday componentsSeparatedByString:@"-"];
        }
        if (array) {
            for (int i = 0; i < [array count]; i++) {
                NSString * s = [array objectAtIndex:i];
                if (i==0) {
                    self.label_year.text=s;
                }
                if (i==1) {
                    self.label_moth.text=s;
                }
                if (i==2) {
                    self.label_day.text=s;
                }
            }
        }
    }
    
    if (self.str_sex_info==nil || [self.str_sex_info isKindOfClass:[NSNull class]]) {
        self.str_sex_info = @"0";
    }
    
    if (self.str_sex_info && [self.str_sex_info isKindOfClass:[NSString class]]) {
        if ([self.str_sex_info isEqualToString:@"0"]) {
            [self girlButtonAction:nil];
        }
        if ([self.str_sex_info isEqualToString:@"1"]) {
            [self boyButtonAction:nil];
        }
    }
}

- (IBAction)goDown:(UIButton *)sender {
    if (sender.tag==2) {
        if (self.label_year.text.intValue-1 < 0) {
            return;
        }
        self.label_year.text = [NSString stringWithFormat:@"%d",self.label_year.text.intValue-1];
        if (YES) {
            NSInteger year_o = self.label_year.text.integerValue;
            NSInteger moth_o = self.label_moth.text.integerValue;
            NSInteger day_o = self.label_day.text.integerValue;
            if (moth_o==2) {
                self.max_day=28;
                if((year_o%4==0&&year_o%100!=0)||(year_o%400==0)) {
                    self.max_day=29;
                }
            } else if (moth_o==1||moth_o==3||moth_o==5||moth_o==7||moth_o==8||moth_o==10||moth_o==12) {
                self.max_day=31;
            } else {
                self.max_day=30;
            }
            if (day_o>self.max_day) {
                self.label_day.text = @"1";
            }
        }
    }
    if (sender.tag==4) {
        if (self.label_moth.text.intValue-1 < 1) {
            self.label_moth.text = [NSString stringWithFormat:@"%d",self.max_moth];
        } else {
            self.label_moth.text = [NSString stringWithFormat:@"%d",self.label_moth.text.intValue-1];
        }
        
        if (YES) {
            NSInteger year_o = self.label_year.text.integerValue;
            NSInteger moth_o = self.label_moth.text.integerValue;
            NSInteger day_o = self.label_day.text.integerValue;
            if (moth_o==2) {
                self.max_day=28;
                if((year_o%4==0&&year_o%100!=0)||(year_o%400==0)) {
                    self.max_day=29;
                }
            } else if (moth_o==1||moth_o==3||moth_o==5||moth_o==7||moth_o==8||moth_o==10||moth_o==12) {
                self.max_day=31;
            } else {
                self.max_day=30;
            }
            if (day_o>self.max_day) {
                self.label_day.text = @"1";
            }
        }
    }
    if (sender.tag==6) {
        if (self.label_day.text.intValue-1 < 1) {
            self.label_day.text = [NSString stringWithFormat:@"%d",self.max_day];
        } else {
            self.label_day.text = [NSString stringWithFormat:@"%d",self.label_day.text.intValue-1];
        }
    }
}

- (IBAction)goUp:(UIButton *)sender {
    if (sender.tag==1) {
        self.label_year.text = [NSString stringWithFormat:@"%d",self.label_year.text.intValue+1];
        
        if (YES) {
            NSInteger year_o = self.label_year.text.integerValue;
            NSInteger moth_o = self.label_moth.text.integerValue;
            NSInteger day_o = self.label_day.text.integerValue;
            if (moth_o==2) {
                self.max_day=28;
                if((year_o%4==0&&year_o%100!=0)||(year_o%400==0)) {
                    self.max_day=29;
                }
            } else if (moth_o==1||moth_o==3||moth_o==5||moth_o==7||moth_o==8||moth_o==10||moth_o==12) {
                self.max_day=31;
            } else {
                self.max_day=30;
            }
            if (day_o>self.max_day) {
                self.label_day.text = @"1";
            }
        }
    }
    if (sender.tag==3) {
        if (self.label_moth.text.intValue+1>self.max_moth) {
            self.label_moth.text = @"1";
        } else {
            self.label_moth.text = [NSString stringWithFormat:@"%d",self.label_moth.text.intValue+1];
        }
        
        if (YES) {
            NSInteger year_o = self.label_year.text.integerValue;
            NSInteger moth_o = self.label_moth.text.integerValue;
            NSInteger day_o = self.label_day.text.integerValue;
            if (moth_o==2) {
                self.max_day=28;
                if((year_o%4==0&&year_o%100!=0)||(year_o%400==0)) {
                    self.max_day=29;
                }
            } else if (moth_o==1||moth_o==3||moth_o==5||moth_o==7||moth_o==8||moth_o==10||moth_o==12) {
                self.max_day=31;
            } else {
                self.max_day=30;
            }
            if (day_o>self.max_day) {
                self.label_day.text = @"1";
            }
        }
    }
    if (sender.tag==5) {
        if (self.label_day.text.intValue+1>self.max_day) {
            self.label_day.text = @"1";
        } else {
            self.label_day.text = [NSString stringWithFormat:@"%d",self.label_day.text.intValue+1];
        }
    }
}

- (IBAction)yearDown:(UIButton *)sender {
    if (_yearLabel.text.intValue<=0) {
        return;
    }
    _yearLabel.text = [NSString stringWithFormat:@"%d",_yearLabel.text.intValue-1];
}

- (IBAction)yearUp:(UIButton *)sender {

    _yearLabel.text = [NSString stringWithFormat:@"%d",_yearLabel.text.intValue+1];
}

- (IBAction)monthDown:(UIButton *)sender {
    if (_monthLabel.text.intValue<=0) {
        return;
    }
    _monthLabel.text = [NSString stringWithFormat:@"%d",_monthLabel.text.intValue-1];
}

- (IBAction)monthUp:(UIButton *)sender {
    if (_monthLabel.text.intValue>=12) {
        _monthLabel.text=@"0";
        _yearLabel.text = [NSString stringWithFormat:@"%d",_yearLabel.text.intValue+1];
        return;
    }
    _monthLabel.text = [NSString stringWithFormat:@"%d",_monthLabel.text.intValue+1];
}

- (IBAction)boyButtonAction:(UIButton *)sender {
    _boyButton.backgroundColor = _segementView.backgroundColor;
    [_boyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _girlButton.backgroundColor = [UIColor whiteColor];
    [_girlButton setTitleColor:_segementView.backgroundColor forState:UIControlStateNormal];
    _sex = YES;
    
    self.str_sex_info = @"1";
}

- (IBAction)girlButtonAction:(UIButton *)sender {
    _girlButton.backgroundColor = _segementView.backgroundColor;
    [_girlButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _boyButton.backgroundColor = [UIColor whiteColor];
    [_boyButton setTitleColor:_segementView.backgroundColor forState:UIControlStateNormal];
    _sex = NO;
    
    self.str_sex_info = @"0";
}

- (IBAction)okButtonAction:(UIButton *)sender {
    /*
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970]-(_yearLabel.text.intValue*365+_monthLabel.text.intValue*30)*24*60*60;
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:time];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    [dateFormatter stringFromDate:date];
    */
    NSString * str_birthday = [NSString stringWithFormat:@"%@-%@-%@",
                               self.label_year.text,
                               self.label_moth.text,
                               self.label_day.text];
    
    NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
               str_birthday, @"baby_birthday",
               self.str_sex_info,@"baby_sex",nil];
    
    [self.view startLoadingAnimation];
    
    [[HttpNewBaseService sharedClient] postRequestOperation:@"member/set_profile" paramDictionary:dic onCompletion:^(NSString *responseString) {
        [self.view stopLoadingAnimation];
        
        NSDictionary * dic = [responseString JSONValue];
        if (dic) {
            NSInteger status = [[dic objectForKey:@"status"] integerValue];
            if (status==1) {
                /*
                UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"修改成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];*/
                [self goBackPage];
                return;
            }
        }
        [self showAlertView:@"温馨提示" subtitle:@"服务器异常!"];

    } onError:^(NSError *error) {
        [self.view stopLoadingAnimation];
        
       [self showAlertView:@"温馨提示" subtitle:@"网络断了!"];
    } onAnimated:YES];
}

- (void)goBackPage
{
    if (self.block_dismiss) {
        NSString * str_birthday = [NSString stringWithFormat:@"%@-%@-%@",
                                   self.label_year.text,
                                   self.label_moth.text,
                                   self.label_day.text];
        self.block_dismiss([self DoMoreThings], self.str_sex_info, str_birthday);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (self.block_dismiss) {
        NSString * str_birthday = [NSString stringWithFormat:@"%@-%@-%@",
                                   self.label_year.text,
                                   self.label_moth.text,
                                   self.label_day.text];
        self.block_dismiss([self DoMoreThings], self.str_sex_info, str_birthday);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setYearAndmonth:(int)year month:(int)nonth babyLabel:(UILabel*)label
{
    _year = [NSString stringWithFormat:@"%d",year];
    _month = [NSString stringWithFormat:@"%d",nonth];
    _tempLabel = label;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSString*)DoMoreThings
{
    NSString * baby_sex = self.str_sex_info;
    NSMutableString * str_info = [[NSMutableString alloc] initWithCapacity:40];
    if (baby_sex) {
        if ([baby_sex isEqualToString:@"0"]) {
            [str_info appendString:@"女  "];
        }
        if ([baby_sex isEqualToString:@"1"]) {
            [str_info appendString:@"男  "];
        }
    } else {
        [str_info appendString:@"没有选择"];
    }
    
    int bo_year=0;
    int bo_month=0;
    
    NSString * baby_birthday = [NSString stringWithFormat:@"%@-%@-%@",
                               self.label_year.text,
                               self.label_moth.text,
                               self.label_day.text];
    
    if (baby_birthday && ![baby_birthday isEqualToString:@""]) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd"];
        NSDate *date1=[formatter dateFromString:baby_birthday];
        
        NSDate *now=[NSDate date];
        //日期减
        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        unsigned int unitFlags = NSDayCalendarUnit;
        NSDateComponents *comps = [gregorian components:unitFlags fromDate:date1  toDate:now  options:0];
        int days = [comps day];
        bo_year = days/365;
        bo_month = days%365/30;
    }
    //...
    [str_info appendFormat:@"%d岁零%d个月", bo_year, bo_month];
    return str_info;
}

@end








